package com.hello.sandbox.core.system;

import android.content.pm.ApplicationInfo;
import android.os.Binder;
import android.os.ConditionVariable;
import android.os.IInterface;
import android.os.Process;
import java.util.Arrays;
import com.hello.sandbox.core.IBActivityThread;
import com.hello.sandbox.entity.AppConfig;
import com.hello.sandbox.proxy.ProxyManifest;

public class ProcessRecord extends Binder {
  public final ApplicationInfo info;
  public final String processName;
  public IBActivityThread bActivityThread;
  public IInterface appThread;
  public int uid;
  public int pid;
  public int buid;
  public int bpid;
  public int callingBUid;
  public int userId;

  public ConditionVariable initLock = new ConditionVariable();

  public ProcessRecord(ApplicationInfo info, String processName) {
    this.info = info;
    this.processName = processName;
  }

  public int getCallingBUid() {
    return callingBUid;
  }

  @Override
  public int hashCode() {
    return Arrays.hashCode(new Object[] {processName, pid, buid, bpid, uid, pid, userId});
  }

  public String getProviderAuthority() {
    return ProxyManifest.getProxyAuthorities(bpid);
  }

  public AppConfig getClientConfig() {
    AppConfig config = new AppConfig();
    config.packageName = info.packageName;
    config.processName = processName;
    config.bpid = bpid;
    config.buid = buid;
    config.uid = uid;
    config.callingBUid = callingBUid;
    config.userId = userId;
    config.token = this;
    return config;
  }

  public void kill() {
    if (pid > 0) {
      try {
        Process.killProcess(pid);
      } catch (Throwable e) {
        e.printStackTrace();
      }
    }
  }

  public String getPackageName() {
    return info.packageName;
  }
}
